package cn.wit.summit.chwan.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.dto.IntentDataBean;
import cn.wit.summit.chwan.models.dto.JumpBaseBean;
import cn.wit.summit.chwan.ui.activity.AllNetHotGameActivity;
import cn.wit.summit.chwan.ui.activity.DailyNewActivity;
import cn.wit.summit.chwan.ui.activity.DailyRecommendationActivity;
import cn.wit.summit.chwan.ui.activity.DiscoveryChoiceListActivity;
import cn.wit.summit.chwan.ui.activity.DownloadCenterActivity;
import cn.wit.summit.chwan.ui.activity.GameDetailV2Activity;
import cn.wit.summit.chwan.ui.activity.GameListIndexActivity;
import cn.wit.summit.chwan.ui.activity.GameStarterActivity;
import cn.wit.summit.chwan.ui.activity.GameTypeListActivity;
import cn.wit.summit.chwan.ui.activity.GreatCrackActivity;
import cn.wit.summit.chwan.ui.activity.LoginV2Activity;
import cn.wit.summit.chwan.ui.activity.MainActivity;
import cn.wit.summit.chwan.ui.activity.MediaTestActivty;
import cn.wit.summit.chwan.ui.activity.ShareWebActivity;
import cn.wit.summit.chwan.ui.activity.SpecialGameDetailActivity;
import cn.wit.summit.chwan.ui.activity.SpecialGamesActivity;
import cn.wit.summit.chwan.ui.activity.ThematicListActivity;
import cn.wit.summit.chwan.ui.activity.TreviFountainListActivity;
import cn.wit.summit.chwan.ui.activity.TreviFountainV2Activity;
import cn.wit.summit.chwan.ui.service.ChWService;
import cn.wit.summit.chwan.ui.service.MyJobIntentService;

/**
 * Created by pengsk on 2018/4/11.
 */

public class IntentUtil {
    private static final IntentUtil ourInstance = new IntentUtil();


    public static IntentUtil getInstance() {
        return ourInstance;
    }

    private IntentUtil() {
    }

    public void intentTo(Activity context, IntentDataBean intentDataBean) {
        Intent intent;
        Bundle bundle;
        switch (intentDataBean.getJump_link_type()) {
            case 1://详情
                goGamedetial(context, intentDataBean.getJump_link_type_val());
                break;
            case 2://合集
                String tplType = intentDataBean.getJump_tpl_type();
                if (!StringUtils.isEmpty(tplType) && tplType.equals("6")) {
                    intent = new Intent(context, GreatCrackActivity.class);
                    intent.putExtra("title", intentDataBean.getJump_title());
                    intent.putExtra("collection_id", intentDataBean.getJump_link_type_val());
                    context.startActivity(intent);
                } else {
                    intent = new Intent(context, SpecialGameDetailActivity.class);
                    String topic_id = intentDataBean.getJump_link_type_val();
                    intent.putExtra("topic_id", topic_id);
                    intent.putExtra("isCollection", true);
                    context.startActivity(intent);
                }
                break;
            case 3://模块
                String jump_link_type_val = intentDataBean.getJump_link_type_val();//跳转需要传的值
                switch (intentDataBean.getJump_type()) {
                    case 1://推荐首页
                        intent = new Intent();
                        intent.setClass(context, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        bundle = new Bundle();
                        bundle.putInt("MainPos", 0);
                        intent.putExtras(bundle);
                        context.startActivity(intent);
                        break;
                    case 2://发现首页
                        intent = new Intent();
                        intent.setClass(context, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        bundle = new Bundle();
                        bundle.putInt("MainPos", 1);
                        intent.putExtras(bundle);
                        context.startActivity(intent);
                        break;
                    case 25://赚钱首页
                        intent = new Intent();
                        intent.setClass(context, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        bundle = new Bundle();
                        bundle.putInt("MainPos", 2);
                        intent.putExtras(bundle);
                        context.startActivity(intent);
                        break;
                    case 3://破解专题
                        goSpecialGamesActivity(context, 1, "破解专题");
                        break;
                    case 4://网游专题
                        goSpecialGamesActivity(context, 2, "网游专题");
                        break;
                    case 5://中文汉化专题
                        goSpecialGamesActivity(context, 3, "中文汉化专题");
                        break;
                    case 6://大型游戏专题
                        goSpecialGamesActivity(context, 4, "大型游戏专题");
                        break;
                    case 7://苹果专区专题
                        goSpecialGamesActivity(context, 5, "苹果专区专题");
                        break;
                    case 8://谷歌专区专题
                        goSpecialGamesActivity(context, 6, "谷歌专区专题");
                        break;
                    case 9://总专题
                        goSpecialGamesActivity(context, 0, "总专题");
                        break;
                    case 10://游戏分类
                        intent = new Intent(context, GameTypeListActivity.class);
                        context.startActivity(intent);
                        break;
                    case 11://个人中心
                    case 12://我的
                        intent = new Intent();
                        intent.setClass(context, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        bundle = new Bundle();
                        bundle.putInt("MainPos", 3);
                        intent.putExtras(bundle);
                        context.startActivity(intent);
                        break;
                    case 14://每日上新
                        intent = new Intent(context, DailyNewActivity.class);
                        context.startActivity(intent);
                        break;
                    case 15://新游首发
                        intent = new Intent(context, GameStarterActivity.class);
                        context.startActivity(intent);
                        break;
                    case 16://每日推荐
                        intent = new Intent(context, DailyRecommendationActivity.class);
                        context.startActivity(intent);
                        break;
                    case 17://'17' => '许愿池'
                        intent = new Intent(context, TreviFountainV2Activity.class);
                        context.startActivity(intent);
                        break;
                    case 18://棋牌游戏专题
                        goSpecialGamesActivity(context, 9, "棋牌游戏专题");
                        break;
                    case 19://女性专题
                        goSpecialGamesActivity(context, 10, "女性专题");
                        break;
                    case 20://休闲专题
                        goSpecialGamesActivity(context, 11, "休闲专题");
                        break;
                    case 21://二次元专题
                        goSpecialGamesActivity(context, 12, "二次元专题");
                        break;
                    case 22://媒体评测
                        intent = new Intent(context, MediaTestActivty.class);
                        intent.putExtra("title", intentDataBean.getJump_title());
                        intent.putExtra("mPosition", intentDataBean.getJump_position());
                        context.startActivity(intent);
                        break;
                    case 23://最热游戏（全网最热游戏）
                        intent = new Intent(context, AllNetHotGameActivity.class);
                        intent.putExtra("mPosition", intentDataBean.getJump_position());
                        context.startActivity(intent);
                        break;
                    case 24://合集信息v2（了不起的破解）
                        intent = new Intent(context, GreatCrackActivity.class);
                        intent.putExtra("title", intentDataBean.getJump_title());
                        intent.putExtra("collection_id", intentDataBean.getJump_link_type_val());
                        context.startActivity(intent);
                        break;
                    case 26://悬赏池
                        context.startActivity(new Intent(context, TreviFountainListActivity.class));
                        break;
                }

                break;
            case 4:
                String url = intentDataBean.getJump_link_type_val();
                goShareWebActivity(context, url);
                break;
            case 5:
                intent = new Intent();
                intent.setAction("android.intent.action.VIEW");
                if (!intentDataBean.getJump_link_type_val().contains("://")) {
                    ToastUtils.getInstance(context).showToastSystem("网络连接错误");
                    return;
                }
                Uri content_url = Uri.parse(intentDataBean.getJump_link_type_val());
                intent.setData(content_url);
                context.startActivity(intent);
                break;
            case 6://游戏直接下载
                intent = new Intent(context, ChWService.class);
                intent.putExtra("downGameId", intentDataBean.getJump_link_type());
                MyJobIntentService.enqueueWork(context, intent);
                break;
            case 7://游戏合集合并
                intent = new Intent(context, ThematicListActivity.class);
                intent.putExtra("title", intentDataBean.getJump_sub_title());
                intent.putExtra("merge_topic_id", intentDataBean.getJump_link_type_val());
                context.startActivity(intent);
                break;
        }
    }

    public void goSpecialGamesActivity(Context context, int belong_type_id, String title) {
        Intent intent = new Intent(context, SpecialGamesActivity.class);
        intent.putExtra("belong_type_id", belong_type_id);
        intent.putExtra("title", title);
        context.startActivity(intent);
    }

    public boolean goLoginInteractive(Context mContext) {
        if (AccountUtil.getInstance().getUidNotToLogin(mContext) == 0)
            return false;
        else return true;

    }

    public void goDiscoveryListActivity(Context context, int type, String title) {
        Intent intent = new Intent(context, DiscoveryChoiceListActivity.class);
        intent.putExtra("type", type);//(1 破解 2 网游 3 中文汉化 4 大型单机 5 苹果专区 6 谷歌专区 7专区 8分类 9 棋牌游戏 10 女性休闲)
        intent.putExtra("title", title);
        context.startActivity(intent);
    }

    public void goShareWebActivity(Context context, String url) {
        Intent intent = new Intent(context, ShareWebActivity.class);
        IntentDataBean dataBean = new IntentDataBean();
        dataBean.setJump_link_type_val(url);
        intent.putExtra("intentData", dataBean);
        context.startActivity(intent);
    }

    public void goMyAccountLoginActivity(Activity context, int i, int i1) {
        goLoginActivity(context);
    }

    public void goLoginActivity(Activity context) {
        Intent intent = new Intent(context, LoginV2Activity.class);
        context.startActivity(intent);
    }

    public void goLoginActivity(Context context) {
        Intent intent = new Intent(context, LoginV2Activity.class);
        context.startActivity(intent);
    }

    public void goAccountCenterActivity(Context context) {

    }

    public void goDownloadCenterActivity(Context context) {
        Intent intent = new Intent(context, DownloadCenterActivity.class);
        context.startActivity(intent);
    }

    public void goGameListIndex(Context context, int game_type, String title) {
        Intent intent = new Intent(context, GameListIndexActivity.class);
        intent.putExtra("type", game_type);
        intent.putExtra("title", title);
        context.startActivity(intent);
    }

    public void goMessageActivity(Context context) {

    }

    public void goSpecialGameDetail(Context context, String topic_id) {
        Intent intent = new Intent(context, SpecialGameDetailActivity.class);
        intent.putExtra("topic_id", topic_id);
        context.startActivity(intent);
    }

    public void intentTo(Activity activity, JumpBaseBean jumpBaseBean) {
        if (jumpBaseBean == null || jumpBaseBean.getJump_info() == null) return;

        IntentDataBean bean = new IntentDataBean();
        if (!TextUtils.isEmpty(jumpBaseBean.getJump_info().getJump_type()))
            bean.setJump_type(Integer.parseInt(jumpBaseBean.getJump_info().getJump_type()));
        if (!TextUtils.isEmpty(jumpBaseBean.getJump_info().getJump_link_type()))
            bean.setJump_link_type(Integer.parseInt(jumpBaseBean.getJump_info().getJump_link_type()));
        if (!TextUtils.isEmpty(jumpBaseBean.getJump_info().getJump_link_type_val()))
            bean.setJump_link_type_val(jumpBaseBean.getJump_info().getJump_link_type_val());
        if (!TextUtils.isEmpty(jumpBaseBean.getJump_info().getJump_tpl_type()))
            bean.setJump_tpl_type(jumpBaseBean.getJump_info().getJump_tpl_type());
        if (!TextUtils.isEmpty(jumpBaseBean.getJump_info().getJump_position()))
            bean.setJump_position(jumpBaseBean.getJump_info().getJump_position());
        bean.setJump_sub_title(jumpBaseBean.getJump_sub_title());
        bean.setJump_title(jumpBaseBean.getJump_title());

        intentTo(activity, bean);
    }

    //formId：1 今日
    public void goGamedetial(Activity activity, String gameid, int formId) {
        if (StringUtils.isEmpty(gameid)) {
            ToastUtils.getInstance(activity).showToastSystem("该游戏数据错误！");
            return;
        }
        Intent intent = new Intent(activity, GameDetailV2Activity.class);
        intent.putExtra("gameID", gameid);
        intent.putExtra("formId", formId);
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.slide_bottom_in, R.anim.top_out);
    }

    public void goGamedetial(Activity activity, String gameid) {
        if (StringUtils.isEmpty(gameid)) {
            ToastUtils.getInstance(activity).showToastSystem("该游戏数据错误！");
            return;
        }
        Intent intent = new Intent(activity, GameDetailV2Activity.class);
        intent.putExtra("gameID", gameid);
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.slide_bottom_in, R.anim.top_out);
    }
}
